﻿using System;
using System.Configuration;

namespace BloggerAutoPosterService.Engine.Utils
{
    public static class ConfigManager
    {
        public static string GetConfigData(string key, bool throwException = true)
        {
            var res = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings[key].Value;
            if (res == null && throwException) throw new Exception(key + " is not defined.");
            return res;
        }

        public static void SetConfigData(string key, string data)
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[key].Value = data;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
